package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.LibListBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.adapter.SetQuesWarehouseAdapter;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.common.SwipeGridView;
import cn.bocweb.spinning.view.dialog.SetQuesWarehouseDialog;
import de.greenrobot.event.EventBus;

public class SetQuesWarehouseActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.gridview)
    SwipeGridView gridview;
    @Bind(R.id.set_warehouse_btn_submit)
    Button setWarehouseBtnSubmit;

    SetQuesWarehouseAdapter adapter;
    List<LibListBean.DataEntity> list;
    SetQuesWarehouseDialog dialog;
    UserPresenter presenter;
    int state;
    String warehouseId;
    String warehouseName;
    int warehouseNum;
    String totalScore;
    String requireScore;
    String timelimit;
    int totalQuiz, selectPosition = 1000;
    @Bind(R.id.set_warehouse_firt_layout)
    LinearLayout setWarehouseFirtLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_ques_warehouse);

        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        initIntent();
        initList();
        initToolbar();
        initClick();
        initData();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    private void initData() {
        presenter = new UserPresenterImp();
        presenter.libList();
        showLoading();
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    private void initClick() {
        setWarehouseBtnSubmit.setOnClickListener(this);
    }

    private void initIntent() {
        Intent intent = getIntent();
        state = intent.getIntExtra(Constant.SET_QUES_WAREHOUSE, Constant.INTENT_EMPTY);
    }

    private void initToolbar() {
        toolbar.setTitle("设置题库");
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        if (state != Constant.FIRST_OPEN_APP_TO_WAREHOUSE) {
            toolbar.setNavigationIcon(R.mipmap.btn_exit);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            setWarehouseFirtLayout.setVisibility(View.GONE);
        } else {
            setWarehouseFirtLayout.setVisibility(View.VISIBLE);
        }
    }

    private void initList() {
        list = new ArrayList<>();
        adapter = new SetQuesWarehouseAdapter(this, list);
        dialog = new SetQuesWarehouseDialog(SetQuesWarehouseActivity.this);
        gridview.setAdapter(adapter);


        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                if (selectPosition != position) {
                    adapter.setSelection(position);
                    adapter.notifyDataSetChanged();

                    warehouseId = list.get(position).getId();
                    warehouseName = list.get(position).getTitle();
                    warehouseNum = list.get(position).getNum();
                    totalQuiz = list.get(position).getTotal();
                    totalScore = list.get(position).getTotalScore();
                    requireScore = list.get(position).getRequireScore();
                    timelimit = list.get(position).getTimelimit();

                    dialog.setContent(warehouseName);
                    dialog.setCount(String.valueOf(warehouseNum));
                    dialog.setId(warehouseName);

                    dialog.setOnDialogClickListener(new SetQuesWarehouseDialog.OnDialogClickListener() {
                        @Override
                        public void click(String id, String content, String count) {
                            dialog.dismiss();
                        }
                    });
                    selectPosition = position;
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.set_warehouse_btn_submit: {
                if (warehouseId != null) {
                    LibListBean.DataEntity dataEntity = new LibListBean.DataEntity();
                    dataEntity.setId(warehouseId);
                    dataEntity.setTitle(warehouseName);
                    dataEntity.setNum(warehouseNum);
                    dataEntity.setRequireScore(requireScore);
                    dataEntity.setTotal(totalQuiz);
                    dataEntity.setTimelimit(timelimit);
                    dataEntity.setTotalScore(totalScore);
                    dataEntity.writeLibListIntoLocal(SetQuesWarehouseActivity.this);
                    if (state == Constant.FIRST_OPEN_APP_TO_WAREHOUSE) {
                        Intent intent = new Intent(SetQuesWarehouseActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        setResult(Constant.SET_WARE_TO_MAIN);
                        finish();
                    }
                } else {
                    DeviceUtil.MyToast(this, "题库未选择");
                }
                break;
            }
        }
    }

    public void onEventMainThread(LibListBean bean) {
        if (bean.getReturn_code() == 0) {
            list.addAll(bean.getData());
            adapter.notifyDataSetChanged();

            warehouseId = list.get(0).getId();
            warehouseName = list.get(0).getTitle();
            warehouseNum = list.get(0).getNum();
            totalQuiz = list.get(0).getTotal();
            totalScore = list.get(0).getTotalScore();
            requireScore = list.get(0).getRequireScore();
            timelimit = list.get(0).getTimelimit();

            LibListBean.DataEntity dataEntity = new LibListBean.DataEntity();
            dataEntity.readLibListIntoLocal(this);
            String libId = dataEntity.getId();
            if (libId != null && !libId.isEmpty()) {
                DeviceUtil.Log("libid = " + libId);
                selectPosition = adapter.initSelectedById(libId);
                warehouseId = list.get(selectPosition).getId();
                warehouseName = list.get(selectPosition).getTitle();
                warehouseNum = list.get(selectPosition).getNum();
                totalQuiz = list.get(selectPosition).getTotal();
                totalScore = list.get(selectPosition).getTotalScore();
                requireScore = list.get(selectPosition).getRequireScore();
                timelimit = list.get(selectPosition).getTimelimit();
                adapter.notifyDataSetChanged();

            }
        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
        }
        hideLoading();
    }
}
